package com.huobao.yunying.goodsdetail_page;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.view.sliding.AbSlidingTabView;
import com.ab.view.titlebar.AbTitleBar;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys.Act_Select_GuigeAndNum;
import com.huobao.yunying.activitys.Act_Zixuan_Daili;
import com.huobao.yunying.entity.DetailCart_data;
import com.huobao.yunying.entity.DetailGoodsData;
import com.huobao.yunying.entity.DetailStoreData;
import com.huobao.yunying.entity.Detail_images;
import com.huobao.yunying.entity.Detailprops;
import com.huobao.yunying.fragments.Tuwen_detail_Fragment;
import com.huobao.yunying.fragments.Tuwen_pinglun_Fragment;
import com.huobao.yunying.fragments.Tuwen_shuxing_Fragment;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;
import com.mcoy.snapscrollview.McoyScrollView;
import com.mcoy.snapscrollview.McoySnapPageLayout;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/12.
 */
public class TuwenPage implements McoySnapPageLayout.McoySnapPage, View.OnClickListener {

	private AbSlidingTabView mAbSlidingTabView;

	/**
	 * 图片列表
	 */
	private List<Detail_images> _images;
	/**
	 * 属性列表
	 */
	private List<Detailprops> props;
	/**
	 * 商品id
	 */
	private String goodsid;
	private DetailGoodsData goodsdata;

	// 店铺数据
	private DetailStoreData storeData;
	// 购物车
	private DetailCart_data cartData;

	private String description;

	private Activity context;
	private View rootView;
	private AbTitleBar mAbTitleBar;

	private McoyScrollView mcoyScrollView;

	private Tuwen_detail_Fragment f1;
	private Tuwen_shuxing_Fragment f2;
	private Tuwen_pinglun_Fragment f3;


	public TuwenPage(Activity context, View rootView, AbTitleBar mAbTitleBar, DetailGoodsData goodsData, DetailStoreData storeData, DetailCart_data cartData) {
		this.context = context;
		this.rootView = rootView;
		this.mAbTitleBar = mAbTitleBar;
		this.goodsdata = goodsData;
		this.storeData = storeData;
		this.cartData = cartData;
		this._images = goodsdata._images;
		this.props = goodsData.props;
		this.goodsid = goodsData.goods_id;
		this.description = goodsData.description;
		initView();
		init();
	}

	private void initView() {
		mAbSlidingTabView = (AbSlidingTabView) rootView.findViewById(R.id.tabview);
		mAbSlidingTabView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				switch(position){
					case 0:
						mcoyScrollView = f1.scrollView;
						break;
					case 1:
						mcoyScrollView = f2.scrollView;
						break;
					case 2:
						mcoyScrollView = f3.scrollView;
						break;
				}
			}

			@Override
			public void onPageSelected(int position) {

			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	@Override
	public View getRootView() {
		return rootView;
	}

	@Override
	public boolean isAtTop() {
		int scrollY = mcoyScrollView.getScrollY();
		return scrollY == 0;
	}

	@Override
	public boolean isAtBottom() {
		return false;
	}


	protected void init() {
		int top = goodsdata.is_collect == 0 ? R.mipmap.icon7 : R.mipmap.icon7_press;
		// 缓存数量
		mAbSlidingTabView.getViewPager().setOffscreenPageLimit(5);
		mAbSlidingTabView.setTabTextColor(Color.BLACK);
		mAbSlidingTabView.setTabSelectColor(Color.rgb(30, 168, 131));
		mAbSlidingTabView.setTabBackgroundResource(R.drawable.tab_bg);
		mAbSlidingTabView.setTabLayoutBackgroundResource(R.mipmap.slide_top);
		mAbSlidingTabView.setTabPadding(20, 8, 20, 8);
		List<String> tabTexts = new ArrayList<String>();
		tabTexts.add("图文详情");
		tabTexts.add("商品属性");
		tabTexts.add("商品评论");
		List<Fragment> mFragments = new ArrayList<Fragment>();
		f1 = Tuwen_detail_Fragment.getInstant(_images);
		f2 = Tuwen_shuxing_Fragment.getInstant(props, description);
		f3 = Tuwen_pinglun_Fragment.getInstant(goodsid);
		mFragments.add(f1);
		mFragments.add(f2);
		mFragments.add(f3);
		mAbSlidingTabView.addItemViews(tabTexts, mFragments);
		mAbSlidingTabView.setCurrentItem(1);
		mAbSlidingTabView.getViewPager().setCurrentItem(1);
		mAbSlidingTabView.setTabPadding(20, 8, 20, 8);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.rtly_top:
//				finish();
				break;
		}
	}

}
